<template>
  <view class="cart">
    <!-- 1 列表 -->
    <view class="cart-list">
      <view
        class="cart-item"
        v-for="item in cartList"
        :key="item.goods_id"
      >
        <!-- 1 复选框 -->
        <view class="goods-chk">
          <u-checkbox
            :value="item.checked"
            shape="circle"
            active-color="red"
            @change="handleToggleCheck(item.goods_id)"
          ></u-checkbox>
        </view>
        <!-- 2 图片 -->
        <view class="goods-img">
          <u-image
            width="191rpx"
            height="191rpx"
            :src="item.goods_small_logo"
          ></u-image>
        </view>
        <!-- 3 信息 -->
        <view class="goods-info">
          <!-- 名称 -->
          <view class="goods-name u-line-2">{{item.goods_name}}</view>
          <!-- 价格和数量 -->
          <view class="goods-price-num">
            <view class="goods-price">￥{{item.goods_price}}</view>
            <view class="goods-num-tool">
              <view
                class="num-btn"
                @click="handleCartNumUpdate(-1,item.goods_id,item.num)"
              >-</view>
              <view class="goods-num">{{item.num}}</view>
              <view
                class="num-btn"
                @click="handleCartNumUpdate(1,item.goods_id,item.num)"
              >+</view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-chk">
        <u-checkbox
          shape="circle"
          active-color="red"
          :value="isAllChecked"
          @change="handleAllCheck"
        >全选</u-checkbox>
      </view>
      <view class="all-price"> 合计: <text>￥{{totalPrice}}</text> </view>
      <view class="all-count">
        <u-button
          type="error"
          shape="circle"
          @click="handleGoPay"
        >去结算({{totalCount}})</u-button>
      </view>
    </view>
  </view>
</template>

<script>
import { mapState, mapMutations, mapGetters } from "vuex";
export default {
  computed: {
      // vuex中购物车数据
      ...mapState("cart", ["cartList"]),
      ...mapGetters("cart", ["isAllChecked", "totalCount", "totalPrice"]),
  },
   methods: {
    ...mapMutations("cart", [
      "cartToggleCheck",
      "cartNumUpdate",
      "cartItemDelete",
      "cartAllToggle",
    ]),

    // 商品单选
    handleToggleCheck(goods_id) {
      console.log(goods_id);
      /* 
      1 把id 传递给 vuex中cart模块 mutations中 方法  商品的切换选中
      
       */

      this.cartToggleCheck({ value: goods_id });
    },
    // 商品数量编辑功能
    async handleCartNumUpdate(unit, goods_id, num) {
      // uni.showModal

      //  uni api  支持 promise

      // 判断用户是否想要删除  unit=-1 && num = 1
      if (unit === -1 && num === 1) {
        // 是否想要删除
        const [err, result] = await uni.showModal({
          title: "提示",
          content: "您是否确认删除",
        });

        if (err) {
          console.log("出错");
          console.log(err);
          return;
        }

        // 用户点击的确定还是取消
        if (result.confirm) {
          // 执行删除
          // console.log("执行删除");
          this.cartItemDelete({ value: goods_id });
        }
      } else {
        // 正确更新数量
        // 1 把id 传递给 vuex中cart模块 mutations中 方法  商品的切换选中
        // 2 unit (-1,+1 )
        this.cartNumUpdate({ value: { unit, goods_id } });
      }
    },

    // 点击全选按钮 切换选中
    handleAllCheck() {
      this.cartAllToggle({ value: this.isAllChecked });
    },

    // 点击  去结算 按钮
    handleGoPay() {
      // 先判断当前有没有选中商品

      if (this.totalCount === 0) {
        // 还没有选购商品 提示
        uni.showToast({
          title: "您还没有选购商品哦",
          icon: "none",
        });
      } else {
        // console.log("跳转吧");
        uni.navigateTo({ url: "/pages/pay/pay" });
      }
    },
  },


}

</script>

<style lang="scss">
.cart {
  padding-bottom: 111rpx;
  .cart-list {
    .cart-item {
      display: flex;
      padding: 10rpx;
      border-bottom: 1rpx solid #ccc;
      .goods-chk {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-img {
        flex: 2;

        display: flex;
        align-items: center;
        justify-content: center;
      }

      .goods-info {
        color: #666;
        flex: 3;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        .goods-name {
        }

        .goods-price-num {
          display: flex;
          justify-content: space-between;
          .goods-price {
            color: $uni-them-color;
          }

          .goods-num-tool {
            width: 140rpx;
            height: 40rpx;

            display: flex;
            justify-content: space-between;
            .num-btn {
              border: 1rpx solid #ccc;
              border-radius: 50%;
              width: 40rpx;
              height: 40rpx;
              text-align: center;
              line-height: 40rpx;
            }
          }
        }
      }
    }
  }
  .statistics {
    position: fixed;
    background-color: #fff;
    bottom: 0;
    left: 0;
    width: 100%;
    z-index: 100;
    border-top: 1rpx solid #ccc;
    display: flex;
    align-items: center;
    padding: 15rpx;
    .all-chk {
    }

    .all-price {
      flex: 1;
      color: #666;
      text {
      }
    }

    .all-count {
    }
  }
}
</style>